package com.hopu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.pojo.PageData;
import com.hopu.pojo.R;
import com.hopu.pojo.Staff;
import com.hopu.service.StaffService;
import com.hopu.vo.StaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YwaiX
 * @since 2024-09-12
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    /**
     * 获取员工列表-袁胡宇
     *
     * @param page
     * @return
     */
    @PostMapping("selectAll")
    public R selectAll(Page<Staff> page, Staff staff) {
        System.out.println(staff);
        PageData<StaffVo> pageData = staffService.selectAll(page, staff);
        return R.ok("获取成功", pageData);
    }

    /**
     * 登录
     *
     * @param staff
     * @return
     */
    @PostMapping("login")
    public R login(Staff staff) {
        return R.ok("登陆成功", this.staffService.login(staff));
    }

    /**
     * 修改员工
     *
     * @param staff 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(Staff staff) {
        staff.setUpdateTime(LocalDateTime.now());
        this.staffService.update(staff);
        return R.ok("更新成功");
    }

    /**
     * 添加员工
     *
     * @param staff
     * @return
     */
    @PostMapping("/addStaff")
    public R insert(Staff staff) {
        System.out.println(staff);
        staff.setJoinDate(LocalDate.now());
        staff.setCreateTime(LocalDateTime.now());
        staff.setStatus(1);
        this.staffService.add(staff);
        return R.ok("添加成功");
    }

    /**
     * 查询员工信息
     *
     * @param staffNo 员工编号
     * @return 员工信息
     */
    @GetMapping("staffInfo")
    public R staffInfo(String staffNo) {
        return R.ok("查询成功", this.staffService.staffInfo(staffNo));
    }

    /**
     * 修改密码和或者手机号
     */
    @PostMapping("updateInfo")
    public R updateInfo(String staffNo, String password, String phone,String realName,String gender,String photoUrl) {
        System.out.println(staffNo + "," + password + "," + phone+","+realName+","+gender+","+photoUrl);
        this.staffService.updateInfo(staffNo, password, phone, realName, gender, photoUrl);
        return R.ok("修改成功");
    }

    /**
     * 修改员工状态
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping("updateStatus")
    public R updateStatus(Integer id, Integer status) {
        System.out.println(id + "," + status);
        staffService.updateStatus(id, status);
        return R.ok("修改成功");
    }

    /**
     * 批量删除用户
     *
     * @param ids
     * @return
     */
    @GetMapping("/deleteStaff/{ids}")
    public R deleteStaff(@PathVariable List<Integer> ids) {
        staffService.deleteStaff(ids);
        return R.ok("操作成功");
    }
}
